﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2_10
{
    public partial class Form1 : Form
    {
        private int[] numbers;
        private Random random = new Random();
        public Form1()
        {

            InitializeComponent();
            // 初始化时禁用排序按钮，直到生成随机数
            btnSortAsc.Enabled = false;
            btnSortDesc.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                int count = int.Parse(txtCount.Text);
                int min = int.Parse(txtMin.Text);
                int max = int.Parse(txtMax.Text);

                if (min >= max)
                {
                    MessageBox.Show("最小值必须小于最大值！");
                    return;
                }

                numbers = new int[count];
                int positiveCount = 0, negativeCount = 0, oddCount = 0, evenCount = 0;

                for (int i = 0; i < count; i++)
                {
                    numbers[i] = random.Next(min, max + 1);

                    // 统计正负数
                    if (numbers[i] >= 0)
                        positiveCount++;
                    else
                        negativeCount++;

                    // 统计奇偶数
                    if (numbers[i] % 2 == 0)
                        evenCount++;
                    else
                        oddCount++;
                }

                // 显示随机数
                txtNumbers.Text = string.Join(Environment.NewLine, numbers);

                // 显示统计结果
                txtStats.Text = $"正数: {positiveCount}\r\n负数: {negativeCount}\r\n奇数: {oddCount}\r\n偶数: {evenCount}";

                // 启用排序按钮
                btnSortAsc.Enabled = true;
                btnSortDesc.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("请输入有效的数值！");
            }
        }

        private void btnSortAsc_Click(object sender, EventArgs e)
        {
            if (numbers == null) return;

            Array.Sort(numbers);
            txtNumbers.Text = string.Join(Environment.NewLine, numbers);
        }

        private void btnSortDesc_Click(object sender, EventArgs e)
        {
            if (numbers == null) return;

            Array.Sort(numbers);
            Array.Reverse(numbers);
            txtNumbers.Text = string.Join(Environment.NewLine, numbers);
        }
    }
}
